package com.gwclh.personnel.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gwclh.personnel.bean.ResultVo;
import com.gwclh.personnel.domain.PageDomain;
import com.gwclh.personnel.entity.OperationLog;
import com.gwclh.personnel.mapper.OperationLogMapper;
import com.gwclh.personnel.service.IOperationLogService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gwclh.personnel.vo.operlogvo.OperLogVo;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 操作记录表
 服务实现类
 * </p>
 *
 * @author gzhhh
 * @since 2020-07-24
 */
@Service
public class OperationLogServiceImpl extends ServiceImpl<OperationLogMapper, OperationLog> implements IOperationLogService {

    /**
     * 分页查询操作记录
     * @param pageDomain
     * @param operLogVo
     * @return
     */
    @Override
    public ResultVo listOperLogs(PageDomain pageDomain, OperLogVo operLogVo) {
        // 设置查询分页参数
        Page<OperationLog> logPage = new Page<>(pageDomain.getPageNum(),pageDomain.getPageSize());
        // 排序方式
        logPage.addOrder(pageDomain.getOrderItemBy());

        // 模糊查询
        QueryWrapper<OperationLog> logQueryWrapper = new QueryWrapper<>();
        logQueryWrapper.like("oper_module",operLogVo.getOperModule())
                .like("oper_user_name",operLogVo.getOperUserName());

        // 一下判断前端是否传递查询参数
        if(operLogVo.getOperType()!=null && StrUtil.isNotEmpty(operLogVo.getOperType())){
            logQueryWrapper.eq("oper_type",operLogVo.getOperType());
        }

        if(operLogVo.getBeginTime()!=null){
            logQueryWrapper.ge("create_time",operLogVo.getBeginTime());
        }
        if(operLogVo.getEndTime()!=null){
            logQueryWrapper.le("create_time",operLogVo.getEndTime());
        }

        if(operLogVo.getOperResult()!=null){
            logQueryWrapper.eq("oper_result",operLogVo.getOperResult());
        }

        this.page(logPage,logQueryWrapper);

        // 返回结果
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("opers",logPage.getRecords());
        resultMap.put("total",logPage.getTotal());

        return ResultVo.success(resultMap);
    }
}
